﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using SWS = System.Web.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CCC = CodeCamp.Controllers;
using e3 = CodeCamp.Model;

namespace CodeCamp.Test.DataConversion
{
    [TestClass]
    public class TestMembershipCreation : TestBase
    {
        private class Tags
        {
            internal const string testUserPW = "testUserPW";
        }

        [TestMethod]
        public void CreateMembershipUserForPresenter()
        {
            string testUserPW = ConfigurationManager.AppSettings[Tags.testUserPW];
            Assert.IsNotNull(testUserPW);
            Assert.AreNotEqual(0, testUserPW.Trim().Length);

            using (CCC.CodeCampDataRepository dr = new CCC.CodeCampDataRepository())
            {

                string userName = null;
                e3.Presenter testPresenter = null;
                foreach (e3.Event ccEvent in dr.EFContext.Events.OrderByDescending(e => e.Id))
                {
                    Assert.IsNotNull(ccEvent);

                    foreach (e3.Presenter presenter in dr.EFContext.Presenters)
                    {
                        Assert.IsNotNull(presenter.UserIdentityClaim);
                        Assert.AreNotEqual(0, presenter.UserIdentityClaim.Trim().Length);
                        userName = presenter.UserIdentityClaim;
                        SWS.MembershipUser user = SWS.Membership.GetUser(userName);
                        if (user == null)
                        {
                            testPresenter = presenter;
                            break;
                        }
                        userName = null;
                    }
                    if (userName != null)
                    {
                        break;
                    }
                }
                if (userName == null)
                    Assert.Inconclusive("All users have Membership entries.");

                Assert.IsNotNull(testPresenter);
                SWS.MembershipCreateStatus status;
                SWS.MembershipUser newUser = SWS.Membership.CreateUser(userName, testUserPW, testPresenter.EmailAddress, null, null, true, out status);
                TestContext.WriteLine("Created new user \"{0}\" with email address \"{1}\".  Status={2}", userName, testPresenter.EmailAddress, status);
                Assert.AreEqual(SWS.MembershipCreateStatus.Success, status);
                Assert.IsNotNull(newUser);
                Assert.AreEqual(testPresenter.UserIdentityClaim, newUser.UserName);
            }
        }


        [TestMethod]
        public void CreateMembershipUsersForPresenter()
        {
            string testUserPW = ConfigurationManager.AppSettings[Tags.testUserPW];
            Assert.IsNotNull(testUserPW);
            Assert.AreNotEqual(0, testUserPW.Trim().Length);

            using (CCC.CodeCampDataRepository dr = new CCC.CodeCampDataRepository())
            {

                int creationCount = 0;
                string userName = null;
                e3.Presenter testPresenter = null;
                foreach (e3.Event ccEvent in dr.EFContext.Events.OrderByDescending(e => e.Id))
                {
                    Assert.IsNotNull(ccEvent);
                    TestContext.WriteLine("Event id={0}\t{1}", ccEvent.Id, ccEvent.ShortName);

                    foreach (e3.Presenter presenter in dr.EFContext.Presenters)
                    {
                        Assert.IsNotNull(presenter.UserIdentityClaim);
                        Assert.AreNotEqual(0, presenter.UserIdentityClaim.Trim().Length);
                        if (presenter.UserIdentityClaim.Trim().Length < presenter.UserIdentityClaim.Length)
                        {
                            TestContext.WriteLine("\t*** Trimmed username \"{0}\". ***", presenter.UserIdentityClaim);
                            presenter.UserIdentityClaim = presenter.UserIdentityClaim.Trim();
                            try
                            {
                                dr.EFContext.SaveChanges();
                            }
                            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                            {
                                ReportException(ex);
                                continue;
                            }
                            catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
                            {
                                ReportException(ex);
                                continue;
                            }
                        }
                        userName = presenter.UserIdentityClaim;
                        SWS.MembershipUser user = SWS.Membership.GetUser(userName);
                        if (user == null)
                        {
                            testPresenter = presenter;
                            Assert.IsNotNull(testPresenter);
                            SWS.MembershipCreateStatus status;
                            SWS.MembershipUser newUser = SWS.Membership.CreateUser(userName, testUserPW, testPresenter.EmailAddress, null, null, true, out status);
                            TestContext.WriteLine("Created new user \"{0}\" with email address \"{1}\".  Status={2}", userName, testPresenter.EmailAddress, status);
                            Assert.AreEqual(SWS.MembershipCreateStatus.Success, status);
                            Assert.IsNotNull(newUser);
                            Assert.AreEqual(testPresenter.UserIdentityClaim, newUser.UserName);
                            creationCount++;
                        }
                        userName = null;
                    }
                }
                TestContext.WriteLine("Created {0} Membership users.", creationCount);
                if (creationCount == 0)
                    Assert.Inconclusive("All users have Membership entries.");
            }

        }

    }
}
